from django.db import models

from sqtp.models.mgr import Project
from sqtp.models.base import BaseModel

'''
null=True	数据库层面允许存储为 NULL
blank=True	表单/序列化器层面可以为空,默认为false，注意为false基本明确了表单那里不会传入null
'''


# Create your models here.
class Config(BaseModel):
    project = models.ForeignKey(Project, on_delete=models.DO_NOTHING,null=True)
    name = models.CharField("名称", max_length=128, null=True)
    # base_url = models.CharField("ip/域名", max_length=256, null=True, blank=True)
    base_url = models.URLField(verbose_name='ip/域名',max_length=256, null=True, blank=True)
    verify = models.BooleanField("https认证", default=False)
    export = models.JSONField("用例返回", null=True)
    variables = models.JSONField("变量", null=True)
    parameters = models.JSONField("参数", null=True)

    def __str__(self):
        return self.name

    class Meta(BaseModel.Meta):  # 模型元类的信息，提供写额外的信息，比如模型对应表名
        # db_table = ['Config'] # 未设置即app名_模型名
        # ordering = ['id']  # 根据id排序
        verbose_name = '用例配置表'


class Case(BaseModel):
    config = models.OneToOneField(Config, on_delete=models.DO_NOTHING)
    file_path = models.CharField("用例文件路径", max_length=1000, default="demo_case.json")

    def __str__(self):
        return self.config.name

    class Meta(BaseModel.Meta):  # 模型元类的信息，提供写额外的信息，比如模型对应表名
        # db_table = ['Config'] # 未设置即app名_模型名
        # ordering = ['id']  # 根据id排序
        verbose_name = '测试用例表'


class Step(BaseModel):
    # 反向查询，同个模型中，两个字段以上关联同一个模型，必须指定related_name
    sorted_no = models.PositiveSmallIntegerField(verbose_name='测试步骤顺序',default=1)
    belong_case = models.ForeignKey(Case, on_delete=models.CASCADE, related_name="teststeps",null=True)  # 属于哪个测试用例
    linked_case = models.ForeignKey(Case, on_delete=models.SET_NULL, related_name="linked_case", null=True)  # 调用了哪个测试用例
    name = models.CharField("测试名", max_length=126, null=True)
    variables = models.JSONField("变量", null=True)
    extract = models.JSONField("请求返回值", null=True)
    validate = models.JSONField("校验", null=True)
    setup_hooks = models.JSONField("初始化", null=True)
    teardown_hooks = models.JSONField("清除", null=True)

    class Meta(BaseModel.Meta):  # 模型元类的信息，提供写额外的信息，比如模型对应表名
        # db_table = ['Config'] # 未设置即app名_模型名
        ordering = ['sorted_no']  # 根据id排序
        verbose_name = '测试步骤表'
        unique_together = ['belong_case', 'sorted_no']  # 同一个用例步骤不一样；belong_case 和 sorted_no 这两个字段的组合 必须是唯一的


class Request(BaseModel):
    method_choices = (
        (0, "GET"),
        (1, "POST"),
        (2, "PUT"),
        (3, "DELETE"),
    )
    step = models.OneToOneField(Step, on_delete=models.CASCADE, null=True,related_name='request')
    method = models.SmallIntegerField(choices=method_choices, default=0)
    url = models.CharField("请求路径", default="/", max_length=1000)
    headers = models.JSONField("请求头", null=True)
    cookies = models.JSONField("Cookies", null=True)
    params = models.JSONField("请求参数", null=True)
    data = models.JSONField("请求体", null=True)
    json = models.JSONField("json格式", null=True)

    def __str__(self):
        return self.url

    class Meta(BaseModel.Meta):  # 模型元类的信息，提供写额外的信息，比如模型对应表名
        # db_table = ['Config'] # 未设置即app名_模型名
        # ordering = ['id']  # 根据id排序
        verbose_name = '请求信息表'
